#ifndef ALTCP_TLS_HITLS_STRUCTS_H
#define ALTCP_TLS_HITLS_STRUCTS_H

#include "lwip/opt.h"

#if defined(LWIP_ALTCP) && defined(LWIP_ALTCP_TLS_HITLS)

#include "lwip/altcp.h"
#include "lwip/pbuf.h"
#include "bsl_uio.h"
#include "hitls.h"
#include "hitls_config.h"
#include "altcp_tls_hitls_opts.h"

#ifdef __cplusplus
extern "C" {
#endif

#define ALTCP_HITLS_FLAGS_HANDSHAKE_DONE    0x01  /* Handshake done */
#define ALTCP_HITLS_FLAGS_UPPER_CALLED      0x02  /* Upper layer called */
#define ALTCP_HITLS_FLAGS_RX_CLOSE_QUEUED   0x04  /* RX close queued */
#define ALTCP_HITLS_FLAGS_RX_CLOSED         0x08  /* RX closed */

typedef struct {
    uint8_t *pass;
    size_t len;
} altcp_hilts_pass_userdata_t;

/**
 * HiTLS configuration structure
 */
typedef struct altcp_tls_config {
    HITLS_Config *hitls_config;   /* HiTLS configuration object */
    uint8_t is_server;            /* Whether it is a server configuration */

#ifdef LWIP_ALTCP_HITLS_AUTH_MODE
    uint8_t auth_mode;            /* Authentication mode: 0-no verification, 1-optional verification, 2-required verification */
#endif

    altcp_hilts_pass_userdata_t *userdata; /* Password user data */
} altcp_tls_config;

/**
 * HiTLS connection state structure
 */
typedef struct altcp_hitls_state_s {
    void *conf;                   /* Configuration pointer */
    HITLS_Ctx *hitls_ctx;        /* HiTLS context */
    BSL_UIO *uio;                /* UIO object */

    /* pbuf chain management */
    struct pbuf *rx;             /* RX data chain (before encryption) */
    struct pbuf *rx_app;         /* Application data chain (after decryption) */

    uint8_t flags;               /* State flags */

    /* Data statistics - used for flow control */
    int32_t rx_passed_unrecved;      /* Unconfirmed received data */
    int32_t uio_bytes_read;          /* UIO read bytes */
    int32_t uio_bytes_appl;          /* Application layer bytes */
    int32_t overhead_bytes_adjust;   /* Overhead bytes adjustment */
} altcp_hitls_state_t;

/**
 * UIO method structure - used to bridge LWIP and HiTLS
 */
typedef struct altcp_hitls_uio_ctx_s {
    struct altcp_pcb *conn;      /* ALTCP connection object */
    altcp_hitls_state_t *state;  /* HiTLS state */
} altcp_hitls_uio_ctx_t;

#ifdef __cplusplus
}
#endif

#endif /* LWIP_ALTCP & LWIP_ALTCP_TLS_HITLS */
#endif /* ALTCP_TLS_HITLS_STRUCTS_H */
